package com.bwda.dsrs.filecenter.controller;


import com.bwda.dsrs.filecenter.domain.po.dbinfo.DbInfoPo;
import com.bwda.dsrs.filecenter.domain.po.scanresultstatistics.ScanResultStatisticsPo;
import com.bwda.dsrs.filecenter.domain.condition.scanresultstatistics.ScanResultStatisticsCondition;
import com.bwda.dsrs.filecenter.domain.form.scanresultstatistics.ScanResultStatisticsQueryForm;
import com.bwda.dsrs.filecenter.domain.form.scanresultstatistics.ScanResultStatisticsUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.scanresultstatistics.ScanResultStatisticsCreateForm;
import com.bwda.dsrs.filecenter.domain.po.scanresulttable.NameCountPo;
import com.bwda.dsrs.filecenter.domain.vo.scanresultstatistics.ScanResultStatisticsVo;
import com.bwda.dsrs.filecenter.service.DbInfoService;
import com.bwda.dsrs.filecenter.service.ScanResultStatisticsService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;


import java.util.List;
import java.util.ArrayList;


import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

@RestController
@RequestMapping("/b/scanresultstatistics")
@Api(value = "/scanresultstatistics", description = "扫描结果统计信息表")
public class ScanResultStatisticsController extends BaseController {

	@Autowired
	private ScanResultStatisticsService scanResultStatisticsService;
	@Autowired
	private DbInfoService dbInfoService;

	@ApiOperation(value = "查询扫描结果统计信息表",notes = "查询扫描结果统计信息表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ScanResultStatisticsVo> query(@ApiParam(value = "db_id", required = true)@RequestParam Integer dbId) throws BusinessServiceException {
		ScanResultStatisticsPo po = scanResultStatisticsService.queryWithValid(dbId);
		ScanResultStatisticsVo vo = CopyUtil.transfer(po, ScanResultStatisticsVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询扫描结果统计信息表数量",notes = "查询扫描结果统计信息表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		ScanResultStatisticsCondition condition = this.getConditionByQueryForm(form);
		int count = scanResultStatisticsService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询扫描结果统计信息表列表",notes = "查询扫描结果统计信息表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<ScanResultStatisticsVo>> queryList(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		ScanResultStatisticsCondition condition = this.getConditionByQueryForm(form);
		List<ScanResultStatisticsPo> poList = scanResultStatisticsService.queryList(condition);
		List<ScanResultStatisticsVo> voList = CopyUtil.transfer(poList, ScanResultStatisticsVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询扫描结果统计信息表列表(带分页)",notes = "查询扫描结果统计信息表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<ScanResultStatisticsVo>> queryPageList(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		ScanResultStatisticsCondition condition = this.getConditionByQueryForm(form);
		List<ScanResultStatisticsVo> voList = new ArrayList<>();
		int count = scanResultStatisticsService.queryCount(condition);
		if (count > 0) {
			List<ScanResultStatisticsPo> poList = scanResultStatisticsService.queryList(condition);
			voList = CopyUtil.transfer(poList, ScanResultStatisticsVo.class);
			if (!CollectionUtils.isEmpty(voList)){
				for (ScanResultStatisticsVo scanResultStatisticsVo:voList
				) {
					DbInfoPo dbInfoPo = dbInfoService.query(scanResultStatisticsVo.getDbId());
					if(dbInfoPo!=null){
						scanResultStatisticsVo.setDbName(dbInfoPo.getDbName());
					}
				}
			}
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增扫描结果统计信息表",notes = "新增扫描结果统计信息表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ScanResultStatisticsVo> add(@ModelAttribute@Valid ScanResultStatisticsCreateForm form) throws BusinessServiceException {
		ScanResultStatisticsPo po = CopyUtil.transfer(form, ScanResultStatisticsPo.class);
		po.setDbId(RandomUtils.nextInt());
		scanResultStatisticsService.insert(po);
		ScanResultStatisticsVo vo = CopyUtil.transfer(po, ScanResultStatisticsVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改扫描结果统计信息表",notes = "修改扫描结果统计信息表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid ScanResultStatisticsUpdateForm form) throws BusinessServiceException {
		ScanResultStatisticsPo po = CopyUtil.transfer(form, ScanResultStatisticsPo.class);
		scanResultStatisticsService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除扫描结果统计信息表",notes = "删除扫描结果统计信息表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "db_id", required = true)@RequestParam Integer dbId) throws BusinessServiceException {
		scanResultStatisticsService.delete(dbId);
		return getSuccessResult();
	}

	@ApiOperation(value = "扫描结果统计",notes = "扫描结果统计",httpMethod = "POST")
	@RequestMapping(value = "/statistics", method = {RequestMethod.GET, RequestMethod.POST})
	public void statistics(@ApiParam(value = "db_id", required = true)@RequestParam Integer dbId) throws BusinessServiceException {
		scanResultStatisticsService.statistics(dbId);
	}


	@ApiOperation(value = "查询扫描结果统计敏感字段信息分布列表",notes = "查询扫描结果统计敏感字段信息分布列表",httpMethod = "GET")
	@RequestMapping(value = "/querySensWordNumList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<NameCountPo>> querySensWordNumList(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		ScanResultStatisticsCondition condition = this.getConditionByQueryForm(form);
		List<NameCountPo> nameCountPosWord  = scanResultStatisticsService.querySensWordNumList(form.getDbId());
		return getSuccessResult(getListResponse(nameCountPosWord));
	}
	@ApiOperation(value = "查询扫描结果统计敏感类型信息分布列表",notes = "查询扫描结果统计敏感类型信息分布列表",httpMethod = "GET")
	@RequestMapping(value = "/querySensTypeNumList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<NameCountPo>> querySensTypeNumList(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		List<NameCountPo> nameCountPosType  = scanResultStatisticsService.querySensTypeNumList(form.getDbId());
		return getSuccessResult(getListResponse(nameCountPosType));
	}
	@ApiOperation(value = "查询扫描结果统计敏感等级信息分布列表",notes = "查询扫描结果统计敏感等级信息分布列表",httpMethod = "GET")
	@RequestMapping(value = "/querySensLevelNumList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<NameCountPo>> querySensLevelNumList(@ModelAttribute@Valid ScanResultStatisticsQueryForm form) throws BusinessServiceException {
		List<NameCountPo> nameCountPosLevel  = scanResultStatisticsService.querySensLevelNumList(form.getDbId());
		return getSuccessResult(getListResponse(nameCountPosLevel));
	}

	/**
	 * ScanResultStatisticsQueryForm转换为ScanResultStatisticsCondition
	 *
	 * @param form
	 * @return
	 */
	private ScanResultStatisticsCondition getConditionByQueryForm(ScanResultStatisticsQueryForm form) {
		ScanResultStatisticsCondition condition = CopyUtil.transfer(form, ScanResultStatisticsCondition.class);
		return condition;
	}

}